﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Product.App.Dtos;
using Product.Domain.Core;

namespace Product.App.AppServices
{
    public class ProductAppService
        : IProductAppService
    {
        private readonly IProductRepository _productRepository;
        public ProductAppService(IProductRepository productRepository)
        {
            _productRepository = productRepository;
        }
        public async Task CreateProductSPUAsync(AddProductSPUInputDto addProductSPU)
        {
            var spuId = Guid.NewGuid();

            var skus = new List<ProductSKU>();
            for (int i = 0; i < addProductSPU.Specs.Count; i++)
            {
                var sku = new ProductSKU(Guid.NewGuid(),spuId,addProductSPU.Specs[i],Enum.Parse<UnitEnum>(addProductSPU.Units[i]),
                    addProductSPU.PVs[i],addProductSPU.DealerPrices[i],addProductSPU.Images[i],addProductSPU.ProductSPUName);
                skus.Add(sku);
            }
            var productSPU = new ProductSPU(spuId,addProductSPU.ProductSPUName,addProductSPU.ProductSPUDesc, skus);
            await _productRepository.CreateProductAsync(productSPU);
            _productRepository.Commit();
        }
    }
}
